<template>
  <div class="container">
    <div class="app-container">
      <!-- data 是数据源 props 配置选项 default-expand-all 默认展开所有节点-->
      <!-- expand-on-click-node 在点击节点时是否收缩或展开 -->
      <el-tree :expand-on-click-node="false" default-expand-all :data="depts" :props="defaultProps">
        <template v-slot="{ data }">
          <el-row style="width: 100%;height:40px;" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="operateDept($event, data.id),operateType ">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 调用弹层子组件 -->
    <!-- .sync 修饰符 可以让子组件既读取父传子的数据,还可以通过update:属性名的事件来改该数据 -->
    <add-dept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDepartment="getDepartment" />
  </div>

</template>
<script>
import { delDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      currentNodeId: null,
      depts: [],
      // 树形组件要展示的名称和子级
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 控制弹层组件
      showDialog: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取组织架构数据
    async getDepartment() {
      // 调用接口
      const result = await getDepartment()
      // 把列表数据转成树形数据
      this.depts = transListToTreeData(result, 0)
    },
    // 下拉菜单的 指令事件
    operateType(type) {
      console.log(type)
      if (type === 'add') {
        this.showDialog = true
      }
    },
    // 操作部门方法
    operateDept(type, id) {
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true // 显示弹层
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.$nextTick(async() => {
          await this.$refs.addDept.getDepartmentDetail()
        })
        this.showDialog = true
        this.currentNodeId = id
      } else {
        this.$confirm('此操作将永久删除该部门，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(id)
          this.$message.success('删除部门成功')
          this.getDepartment()
        }).catch(() => {
          console.log('取消了')
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
  .tree-manager {
    display: inline-block;
    width: 50px;
    margin: 10px;
  }
}
</style>
